package com.own.business.level.business.aspect;

import com.own.business.level.business.aop.LevelAop;
import com.own.business.level.business.core.ExpRuleRun;
import com.own.business.level.business.core.LevelSpringHook;
import com.own.component.common.login.util.SessionUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * LevelAspect
 *
 * @author chenxueli
 * @date 2022-08-17 17:25:00
 */
@Slf4j
@Aspect
@Component
@Order(0)
public class LevelAspect {

    @Resource
    private LevelSpringHook levelSpringHook;
    @Resource
    private ExpRuleRun expRuleRun;

    @Pointcut("@annotation(com.own.business.level.business.aop.LevelAop)")
    public void aspect() {
    }

    @Before("aspect() && @annotation(data)")
    public void doBefore(JoinPoint point, LevelAop data) {
        if (data.isAfter()) {
            return;
        }
        run(data);
    }

    @After("aspect() && @annotation(data)")
    public void doAfter(JoinPoint point, LevelAop data) {
        if (data.isAfter()) {
            run(data);
        }
    }

    /**
     * 执行方法
     *
     * @param data 注解数据
     */
    private void run(LevelAop data) {
        // 获取对应的增加方法的信息
        var util = levelSpringHook.getByName(data.type());
        if (util == null) {
            return;
        }
        // 执行方法
        expRuleRun.run(util, SessionUserUtil.loginUser());
    }

}
